/**
 * Created by blzx on 2017/1/16.
 */
var time = 60;
var btnSend;
var timer;
$(document).ready(function () {
    btnSend = $("#send");
    btnSend.click(function () {
    	var phonenum = $("#tel").val();
	    if(!isPhoneNum(phonenum)){
	    	layer.open({
        	    content: '用户手机号填写不正确，请重新输入',
        	    btn: '我知道了'
        	});
	    	return;
		}
	    $.post("/admin/sendCode4BindApp.action", {
	     	'phone': phonenum
	      }, function (result) {
	          result = JSON.parse(result)
	          if (result.code == 1) {
	        	  layer.open({
	          	    content: '已发送',
	          	    btn: '我知道了'
	          	});
	        	  btnSend.html("剩余 " + time + " 秒");
	              btnSend.attr("disabled", "disabled");
	              setTimeout(function () {
	                  checkTime()
	              }, 1000);
	          }else{
	        	  layer.open({
		          	    content: result.msg,
		          	    btn: '我知道了'
		          	});
	        	  time = 60;
	              btnSend.removeAttr("disabled");
	              btnSend.html("获取验证码");
	              window.clearInterval(timer);
	          }
	      });
    });
});

//校验手机号是否合法
function isPhoneNum(phonenum){
    var myreg = /^1[3|4|5|7|8]\d{9}$/; 
    if(!myreg.test(phonenum)){ 
        return false; 
    }else{
        return true;
    }
}
function checkTime() {
    if (time == 0) {
        time = 60;
        btnSend.removeAttr("disabled");
        btnSend.html("获取验证码");
        window.clearInterval(timer);
    } else {
        time--;
        btnSend.html("剩余 " + time + " 秒");
        setTimeout(function () {
            checkTime()
        }, 1000);
    }
}